package com.lzw.app;
import android.os.*;
import android.content.*;
import java.io.*;
import java.text.*;
import java.util.*;
import android.content.pm.*;
import android.widget.*;
import android.util.*;
import android.app.*;
public class mCrash implements Thread.UncaughtExceptionHandler
{

    //文件夹目录
    private static final String PATH = Environment.getExternalStorageDirectory().getPath() + "/Android/data/" + myApp.getContext().getPackageName() + "/";
    //文件名
    private static final String FILE_NAME = "crash";
    //文件名后缀
    private static final String FILE_NAME_SUFFIX = ".trace";
    //上下文
    private Context mContext;

    //单例模式
    private static mCrash sInstance = new mCrash();
    private mCrash()
	{}
    public static mCrash getInstance()
	{
        return sInstance;
    }

    /**
     * 初始化方法
     *
     * @param context
     */
    public void init(Context context)
	{
        //将当前实例设为系统默认的异常处理器
        Thread.setDefaultUncaughtExceptionHandler(this);
        //获取Context，方便内部使用
        mContext = context;
    }

    /**
     * 捕获异常回掉
     *
     * @param thread 当前线程
     * @param ex     异常信息
     */
    @Override
    public void uncaughtException(Thread thread, Throwable ex)
	{
        //导出异常信息到SD卡
        dumpExceptionToSDCard(ex);
        //上传异常信息到服务器
        uploadExceptionToServer(ex);
		//显示错误提示框
		new AlertDialog.Builder(mContext).setTitle("APP异常").setMessage("很抱歉").show();
        //延时1秒杀死进程
        SystemClock.sleep(2000);
		android.os.Process.killProcess(android.os.Process.myPid());
    }

    /**
     * 导出异常信息到SD卡
     *
     * @param ex
     */
    private void dumpExceptionToSDCard(Throwable ex)
	{
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
		{
            return;
        }
        //创建文件夹
        File dir = new File(PATH);
        if (!dir.exists())
		{
            dir.mkdirs();
        }
        //获取当前时间
        long current = System.currentTimeMillis();
        String time = new SimpleDateFormat("yyyy-MM-dd--HH-mm-ss").format(new Date(current));
        //以当前时间创建log文件
        File file = new File(PATH + FILE_NAME + time + FILE_NAME_SUFFIX);
		try
		{
			if (!file.exists())
				file.createNewFile();
		}
		catch (IOException e)
		{}
        try
		{
            //输出流操作
			PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
			FileOutputStream fo = new FileOutputStream(file);
            //导出手机信息和异常信息
            PackageManager pm = mContext.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
			pw.println("发生异常时间：" + time);
			pw.println("应用版本：" + pi.versionName);
			pw.println("应用版本号：" + pi.versionCode);
			pw.println("android版本号：" + Build.VERSION.RELEASE);
			pw.println("android版本号API：" + Build.VERSION.SDK_INT);
			pw.println("手机制造商:" + Build.MANUFACTURER);
			pw.println("手机型号：" + Build.MODEL);
			ex.printStackTrace(pw);
            //关闭输出流
            pw.close();
        }
		catch (Exception e)
		{
			e.printStackTrace();
        }
    }

    /**
     * 上传异常信息到服务器
     *
     * @param ex
     */
    private void uploadExceptionToServer(Throwable ex)
	{
		String e = ex.getMessage();
    }
}
